﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    internal class StringUtil {
        private static readonly Option[] optEmpty = new Option[0];
        private static readonly char[]   splitStr = new char[] { '|' };

        

        public static Option[] GetOptions(string options) {
            if (String.IsNullOrEmpty(options))
                return optEmpty;

            string[] stropts = options.Split(splitStr, StringSplitOptions.RemoveEmptyEntries);
            Option[] opts     = new Option[stropts.Length];
            int      index   = 0;

            foreach (string stropt in stropts){
                opts[index++] = new Option(stropt);
            }

            return opts;
        }

        internal class Option {
            private string _id;
            private string _description;

            internal Option(string option) {
                int pos = option.IndexOf('=');
                if (pos > -1) {
                    _id = option.Substring(0, pos);
                    _description = option.Substring(pos + 1);
                } else {
                    _id = option;
                }
            }

            public string Id { get { return _id; } }
            public string Description { get { return _description; } }

            public override bool Equals(object obj) {
                if (obj == null) {
                    return false;
                }

                Option p = obj as Option;
                if ((System.Object)p == null) {
                    return false;
                }

                return (_id.Equals(p._id));
            }

            public override int GetHashCode() {
                return _id.GetHashCode();
            }

            public override string ToString() {
                return _description;
            }

        }

    }

    
}
